﻿Public Class MainForm

    Private _myConfig As New MyConfig
    Private _ds As New CSDN1000DataSet
    Private _ds采集 As New CSDN1000DataSet
    Private _ds访问 As New CSDN1000DataSet
    Private _key As String = ""
    Private _打招呼间隔时间 As Decimal = 1000 * 60 * 24 * 6 ' 一个星期
    Private _访问空间间隔时间 As Decimal = 1000 * 60 * 24 * 1 ' 一天
    Private _留言间隔时间 As Decimal = 1000 * 60 * 24 * 6 * 4 ' 一个月
    Private _采集个数 As Integer = 0

    Private _访问空间个数 As Integer = 0
    Private _留言个数 As Integer = 0
    Private _打招呼个数 As Integer = 0
    Private _添加朋友个数 As Integer = 0
    Private _关注个数 As Integer = 0

    Private Sub 关于ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于ToolStripMenuItem.Click
        Using frm As New AboutBox1
            frm.ShowDialog()
        End Using
    End Sub

    Private Sub 用户名ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 用户名ToolStripMenuItem.Click
        Using frm As New LoginForm(Me._myConfig)
            frm.ShowDialog()
        End Using
        If _myConfig.是否使用代理服务器 = True Then
            Dim wp As New Net.WebProxy(_myConfig.IP地址, CInt(_myConfig.IP端口))
            CSDNLOGIN._WebProxy = wp
        Else
            CSDNLOGIN._WebProxy = Nothing
        End If
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: 这行代码将数据加载到表“CSDN1000DataSet.CSDN1000”中。您可以根据需要移动或删除它。
        Dim C2 As DataColumn = _ds采集.CSDN1000.Columns.Add("NO")
        C2.AutoIncrement = True
        C2.AutoIncrementSeed = 1
        C2.AutoIncrementStep = 1
        Me.CSDN1000TableAdapter.FillBy采集(_ds采集.CSDN1000)

        Dim cl As DataColumn = _ds访问.CSDN1000.Columns.Add("NO")
        cl.AutoIncrement = True
        cl.AutoIncrementSeed = 1
        cl.AutoIncrementStep = 1

        Me.CSDN1000TableAdapter.Fill(_ds访问.CSDN1000)

        Me.gv访问.DataSource = Nothing
        Me.gv采集.DataSource = Nothing

        Me.gv访问.DataSource = _ds访问.CSDN1000
        Me.gv采集.DataSource = _ds采集.CSDN1000

        Control.CheckForIllegalCrossThreadCalls = False
        _ds.EnforceConstraints = False

        _key = Now.ToString("yyyyMMdd")

        Dim count As Integer = 0

        CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.访问空间的个数, count)
        If count > CInt(_myConfig.每天访问空间的个数) Then
            Me.chk访问空间.Checked = False
            Me.chk访问空间.Enabled = False
        End If

        CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.关注的个数, count)
        If count > CInt(_myConfig.每天加为关注的个数) Then
            Me.chk添加关注.Checked = False
            Me.chk添加关注.Enabled = False
        End If

        CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.加为朋友的个数, count)
        If count > CInt(_myConfig.每天加为朋友的个数) Then
            Me.chk添加好友.Checked = False
            Me.chk添加好友.Enabled = False
        End If

        CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.打招呼的个数, count)
        If count > CInt(_myConfig.每天打招呼的个数) Then
            Me.chk打招呼.Checked = False
            Me.chk打招呼.Enabled = False
        End If

        CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.留言个数, count)
        If count > CInt(_myConfig.每天的留言个数) Then
            Me.chk留言.Checked = False
            Me.chk留言.Enabled = False
        End If

        If _myConfig.是否使用代理服务器 = True Then
            Dim wp As New Net.WebProxy(_myConfig.IP地址, CInt(_myConfig.IP端口))
            CSDNLOGIN._WebProxy = wp
        Else
            CSDNLOGIN._WebProxy = Nothing
        End If

        If Me._myConfig.最大朋友 = "1" Then
            Me.chk添加好友.Enabled = False
            Me.chk添加好友.Checked = False
        End If

        If _myConfig.最大关注 = "1" Then
            Me.chk添加关注.Enabled = False
            Me.chk添加关注.Checked = False
        End If

    End Sub

#Region "采集功能"
    Private Sub btn采集_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn采集.Click
        '登陆
        'Dim bLogin As Boolean = CSDNLOGIN.Login(_myConfig.用户名, _myConfig.密码)
        'If bLogin Then
        '    Dim lstFriend As New List(Of MyFriend)
        '    Dim lstComment As New List(Of MyFriend)

        'If CSDNLOGIN.LoginBlog("http://hi.csdn.net/space.php?do=home", lstFriend, lstComment) Then
        '    If lstFriend.Count > 0 Then
        '        _ds.CSDN1000.Rows.Clear()
        '        For Each myFrend As MyFriend In lstFriend
        '            Dim dr As CSDN1000DataSet.CSDN1000Row = Me._ds.CSDN1000.NewCSDN1000Row
        '            dr.ID = myFrend.id
        '            dr.NAME = myFrend.name
        '            dr.username = myFrend.username
        '            dr.BLOGADDRESS = "http://blog.csdn.net/" & myFrend.username
        '            dr.SPACEADDRESS = myFrend.Url
        '            dr.IsComment = False
        '            dr.IsFriend = False
        '            dr.IsSayHello = False
        '            dr.AddTime = Now.ToString("yyyyMMddHHmmss")
        '            dr.UpdateTime = dr.AddTime
        '            dr.LastAccessTime = dr.AddTime
        '            Me._ds.CSDN1000.AddCSDN1000Row(dr)
        '        Next
        '    End If
        '    CSDNBL.UpdateData(Me._ds)
        '    System.Threading.Thread.Sleep(1000)
        'End If

        'For Each dgr As DataGridViewRow In gv采集.Rows
        '    lst采集.Items.Insert(0, dgr.Cells("SPACEADDRESS").Value.ToString())
        '    dgr.Selected = True
        '    Application.DoEvents()
        '    If CSDNLOGIN.LoginSpace(dgr.Cells("SPACEADDRESS").Value.ToString(), lstFriend, lstComment) Then
        '        If lstFriend.Count > 0 Then
        '            _ds.CSDN1000.Rows.Clear()
        '            For Each myFrend As MyFriend In lstFriend
        '                Dim dr As CSDN1000DataSet.CSDN1000Row = Me._ds.CSDN1000.NewCSDN1000Row
        '                dr.ID = myFrend.id
        '                dr.NAME = myFrend.name
        '                dr.username = myFrend.username
        '                dr.BLOGADDRESS = ""
        '                dr.SPACEADDRESS = myFrend.Url
        '                dr.IsComment = False
        '                dr.IsFriend = False
        '                dr.IsSayHello = False
        '                dr.AddTime = Now.ToString("yyyyMMddHHmmss")
        '                dr.UpdateTime = 0
        '                dr.LastAccessTime = 0
        '                Me._ds.CSDN1000.AddCSDN1000Row(dr)
        '            Next
        '        End If
        '        CSDNBL.UpdateData(Me._ds)
        '        System.Threading.Thread.Sleep(1000)
        '    End If

        'Next

        'End If
        _采集个数 = 0

        If gv采集.RowCount = 0 Then
            Exit Sub
        End If
        PB采集.Maximum = gv采集.RowCount
        PB采集.Minimum = 0
        PB采集.Value = 0
        _采集 = True
        btn采集.Enabled = False
        btn结束采集.Enabled = True
        Application.DoEvents()
        BW采集.RunWorkerAsync()
    End Sub

#End Region

    Private Sub btn结束采集_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn结束采集.Click
        _采集 = False
    End Sub

    Private _访问中 As Boolean = False
    Private Sub btn开始访问_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn开始访问.Click
        _访问空间个数 = 0
        _留言个数 = 0
        _打招呼个数 = 0
        _添加朋友个数 = 0
        _关注个数 = 0

        If gv访问.RowCount = 0 Then
            Exit Sub
        End If
        If chk访问空间.Checked = False AndAlso _
        chk打招呼.Checked = False AndAlso _
        chk添加好友.Checked = False AndAlso _
        chk添加关注.Checked = False AndAlso _
        chk留言.Checked = False Then
            MessageBox.Show("请至少选择一个进行操作", Me.Text)
            Return
        End If

        PB访问.Maximum = gv访问.RowCount
        PB访问.Minimum = 0
        PB访问.Value = 0
        _访问中 = True
        btn开始访问.Enabled = False
        btn停止访问.Enabled = True
        Application.DoEvents()
        BW访问.RunWorkerAsync()
    End Sub

    Private Sub btn停止访问_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn停止访问.Click
        _访问中 = False
        Application.DoEvents()
    End Sub

    Private Sub Add访问(ByVal strItem As String)
        lst访问.Items.Insert(0, strItem)
    End Sub

    Private Sub Add采集(ByVal strItem As String)
        lst采集.Items.Insert(0, strItem)
    End Sub

    Public Function GetRandom() As Integer
        Return New Random().Next(700, 2500)
    End Function

    Public Function GetRandom留言() As Integer
        Return New Random().Next(101000, 145000)
    End Function

    Private Sub BW采集_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BW访问.DoWork
        '登陆
        Add访问("开始登陆......")
        Dim bLogin As Boolean = CSDNLOGIN.Login(_myConfig.用户名, _myConfig.密码)
        If bLogin Then
            Add访问("登陆成功")
        Else
            Add访问("登陆失败")
            Exit Sub
        End If

        Dim 空间 As String = ""
        Dim 用户 As String = ""
        Dim 博客地址 As String = ""
        Dim 休息时间 As Integer = 0
        Dim 打招呼返回内容 As String = ""
        Dim LASTSayHello As String = "0"
        Dim LastAccessTime As String = "0"
        Dim LastCommentTime As String = "0"
        Dim LastFriend As String = "0"
        Dim LastSetFocus As String = "0"

        Dim 现在时间 As String = Now.ToString("yyyyMMddHHmmss")
        Dim count As Integer = 0
        Dim count_访问空间 As Integer = 0
        Dim count_加朋友 As Integer = 0
        Dim count_休息 As Integer = 0
        Dim 是否添加关注 As Boolean = False
        Dim 是否打过招呼 As Boolean = False
        Dim 是否加个朋友 As Boolean = False

        For idex As Integer = 0 To gv访问.RowCount - 1
            Try
                If chk访问空间.Checked = False AndAlso _
                chk打招呼.Checked = False AndAlso _
                chk添加好友.Checked = False AndAlso _
                chk添加关注.Checked = False AndAlso _
                chk留言.Checked = False Then
                    Exit For
                End If

                LBL访问.Text = " 访问了" & Me._访问空间个数 & "个空间" & " 关注了" _
                              & Me._关注个数 & "人" & " 添加了" _
                              & Me._添加朋友个数 & "个好友" _
                              & " 打了" & Me._打招呼个数 & "个招呼" _
                              & " 留了" & Me._留言个数 & "次言"

                gv访问.Rows(idex).Selected = True
                gv访问.FirstDisplayedScrollingRowIndex = idex

                '连续访问50个休息10秒
                If count_访问空间 Mod 20 = 0 AndAlso count_访问空间 > 0 Then
                    Add访问("连续访问20个 休息 10 秒 防止拒绝访问")
                    System.Threading.Thread.Sleep(10000)
                    count_休息 += 10000
                End If

                '访问空间
                'If chk访问空间.Checked Then
                '    CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.访问空间的个数, count)
                '    If count > CInt(_myConfig.每天访问空间的个数) Then
                '        chk访问空间.Enabled = False
                '        chk访问空间.Checked = False
                '        Add访问("今天已经到了访问限制次数了")
                '    Else
                '        LastAccessTime = CStr(gv访问.Rows(idex).Cells("访问LastAccessTime").Value)
                '        If LastAccessTime = 0 OrElse UTIL.Get两个时间间隔(现在时间, LastAccessTime) > _访问空间间隔时间 Then
                '            空间 = gv访问.Rows(idex).Cells("访问空间列").Value.ToString
                '            休息时间 = GetRandom()
                '            count_休息 += 休息时间
                '            Add访问("随机休息" & 休息时间 & "......")
                '            Threading.Thread.Sleep(休息时间)
                '            If CSDNLOGIN.登陆空间(空间, gv访问.Rows(idex).Cells("访问名称列").Value.ToString, 用户, 博客地址) Then
                '                Add访问("用户" & gv访问.Rows(idex).Cells("访问名称列").Value.ToString & " 访问成功")
                '                If chk更新用户名.Checked Then
                '                    CSDNBL.UpdateLastAccessTime(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 用户, 博客地址)
                '                    Add访问("更新成功 用户名 " & 用户 & " " & 博客地址)
                '                End If
                '                _访问空间个数 += 1
                '                gv访问.Rows(idex).Cells("访问空间").Value = "成功"
                '                count_访问空间 += 1
                '                CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.访问空间的个数)
                '                '开始更新 界面
                '            Else
                '                gv访问.Rows(idex).Cells("访问空间").Value = "失败"
                '            End If
                '        Else
                '            'Add访问("用户" & gv访问.Rows(idex).Cells("访问名称列").Value.ToString & " 一天内 空间已经访问过")
                '            gv访问.Rows(idex).Cells("访问空间").Value = "已经访问"
                '        End If
                '    End If
                'End If

                '打招呼
                'If chk打招呼.Checked Then
                '    CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.打招呼的个数, count)
                '    If count > CInt(_myConfig.每天打招呼的个数) Then
                '        chk打招呼.Enabled = False
                '        chk打招呼.Checked = False
                '        Add访问("今天已经到了访问限制次数了")
                '    Else
                '        If chk访问空间.Checked Then
                '            '访问空间超过15 打个招呼
                '            If count_访问空间 Mod 10 = 0 AndAlso count_访问空间 > 0 Then
                '                打招呼返回内容 = ""
                '                LASTSayHello = CStr(gv访问.Rows(idex).Cells("LASTSayHello").Value)
                '                If LASTSayHello = 0 OrElse UTIL.Get两个时间间隔(现在时间, LASTSayHello) > _打招呼间隔时间 Then
                '                    空间 = gv访问.Rows(idex).Cells("访问空间列").Value.ToString
                '                    If CSDNLOGIN.打招呼(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 空间, 打招呼返回内容) _
                '                        AndAlso 打招呼返回内容.Contains("已经") _
                '                        AndAlso 打招呼返回内容.Trim.Length > 0 Then
                '                        CSDNBL.UpdateLASTSayHello(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                '                        CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.打招呼的个数)
                '                    End If
                '                    Add访问(打招呼返回内容)
                '                    gv访问.Rows(idex).Cells("访问打招呼列").Value = "成功"
                '                    _打招呼个数 += 1
                '                Else
                '                    gv访问.Rows(idex).Cells("访问打招呼列").Value = "已经打过"
                '                End If
                '            End If
                '        Else
                '            打招呼返回内容 = ""
                '            LASTSayHello = CStr(gv访问.Rows(idex).Cells("LASTSayHello").Value)
                '            If LASTSayHello = 0 OrElse UTIL.Get两个时间间隔(现在时间, LASTSayHello) > _打招呼间隔时间 Then
                '                空间 = gv访问.Rows(idex).Cells("访问空间列").Value.ToString
                '                休息时间 = GetRandom()
                '                Add访问("随机休息" & 休息时间 & "......")
                '                Threading.Thread.Sleep(休息时间)
                '                If CSDNLOGIN.打招呼(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 空间, 打招呼返回内容) _
                '                    AndAlso 打招呼返回内容.Contains("已经") _
                '                    AndAlso 打招呼返回内容.Trim.Length > 0 Then
                '                    CSDNBL.UpdateLASTSayHello(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                '                    CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.打招呼的个数)
                '                End If
                '                Add访问(打招呼返回内容)
                '                gv访问.Rows(idex).Cells("访问打招呼列").Value = "成功"
                '                _打招呼个数 += 1
                '            Else
                '                'Add访问("用户" & gv访问.Rows(idex).Cells("访问名称列").Value.ToString & " 最近一星期已经打过招呼")
                '                gv访问.Rows(idex).Cells("访问打招呼列").Value = "已经打过"
                '            End If
                '        End If
                '    End If
                'End If

                ''添加好友
                'If chk添加好友.Checked Then
                '    CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.加为朋友的个数, count)
                '    If count > CInt(_myConfig.每天加为朋友的个数) Then
                '        chk添加好友.Enabled = False
                '        chk添加好友.Checked = False
                '        Add访问("今天已经到了访问限制次数了")
                '    Else
                '        打招呼返回内容 = ""
                '        LastFriend = CStr(gv访问.Rows(idex).Cells("LastFriend").Value)
                '        If LastFriend = 0 Then
                '            空间 = gv访问.Rows(idex).Cells("访问空间列").Value.ToString
                '            休息时间 = GetRandom()
                '            Add访问("随机休息" & 休息时间 & "......")
                '            Threading.Thread.Sleep(休息时间)
                '            If CSDNLOGIN.添加好友(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 空间, 打招呼返回内容) _
                '                AndAlso 打招呼返回内容.Contains("好友请求已经发送，请等待对方验证中") _
                '                AndAlso 打招呼返回内容.Trim.Length > 0 Then
                '                CSDNBL.UpdateLASTFriend(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                '                CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.加为朋友的个数)
                '                Add访问(打招呼返回内容)
                '                gv访问.Rows(idex).Cells("访问好友列").Value = "成功"
                '                _添加朋友个数 += 1
                '            Else
                '                If 打招呼返回内容.Contains("您当前的好友数目达到系统限制，请先删除部分好友") Then
                '                    Me.chk添加好友.Checked = False
                '                    Me.chk添加好友.Enabled = False
                '                    Add访问("您当前的好友数目达到系统限制，请先删除部分好友")
                '                End If
                '                Add访问(打招呼返回内容)
                '                gv访问.Rows(idex).Cells("访问好友列").Value = "失败"
                '            End If
                '        Else
                '            gv访问.Rows(idex).Cells("访问好友列").Value = "已经发送过"
                '        End If
                '    End If
                'End If

                    '添加关注
                'If chk添加关注.Checked Then
                '    CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.关注的个数, count)
                '    If count > CInt(_myConfig.每天加为关注的个数) Then
                '        chk添加关注.Enabled = False
                '        chk添加关注.Checked = False
                '        Add访问("今天已经到了访问限制次数了")
                '    Else

                '    End If
                'End If

                    '留言
                If chk留言.Checked Then
                    '估计的十五分钟
                    If _留言个数 Mod 7 = 0 AndAlso _留言个数 > 0 Then
                        Add访问("连续添加了7个休息8分钟。。。。。。")
                        Threading.Thread.Sleep(1000 * 60 * 8)
                    End If

                    '每三次留言以后添加一次关注
                    If chk添加关注.Checked AndAlso _留言个数 Mod 3 = 0 AndAlso _留言个数 > 0 AndAlso 是否添加关注 = False Then
                        LastSetFocus = CStr(gv访问.Rows(idex).Cells("LastSetFocus").Value)
                        If LastSetFocus = 0 OrElse UTIL.Get两个时间间隔(现在时间, LastSetFocus) > _留言间隔时间 Then
                            休息时间 = GetRandom()
                            Add访问("随机休息" & 休息时间 & "......")
                            Threading.Thread.Sleep(休息时间)
                            If CSDNLOGIN.关注(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 打招呼返回内容) _
                                AndAlso 打招呼返回内容.Contains("添加关注成功") _
                                AndAlso 打招呼返回内容.Trim.Length > 0 Then
                                CSDNBL.UpdateLASTLastSetFocus(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                gv访问.Rows(idex).Cells("访问关注列").Value = "成功"
                                CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.关注的个数)
                                _关注个数 += 1
                            ElseIf 打招呼返回内容.Contains("您当前的关注数目达到系统限制，请先删除部分关注") Then
                                chk添加关注.Checked = False
                                chk添加关注.Enabled = False
                                _myConfig.最大关注 = "1"
                            Else
                                CSDNBL.UpdateLASTLastComment1(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                gv访问.Rows(idex).Cells("访问关注列").Value = "失败"
                            End If
                            Add访问(打招呼返回内容)
                        Else
                            gv访问.Rows(idex).Cells("访问关注列").Value = "已经留言过"
                        End If
                        是否添加关注 = True
                        Continue For '这条记录不添加留言了
                    Else
                        If chk添加关注.Checked = True AndAlso _留言个数 Mod 3 = 0 AndAlso _留言个数 > 0 Then
                            是否添加关注 = False
                        End If
                    End If

                    '每四次留言以后打一次招呼
                    If chk打招呼.Checked AndAlso _留言个数 Mod 4 = 0 AndAlso _留言个数 > 0 AndAlso 是否打过招呼 = False Then
                        打招呼返回内容 = ""
                        LASTSayHello = CStr(gv访问.Rows(idex).Cells("LASTSayHello").Value)
                        If LASTSayHello = 0 OrElse UTIL.Get两个时间间隔(现在时间, LASTSayHello) > _打招呼间隔时间 Then
                            空间 = gv访问.Rows(idex).Cells("访问空间列").Value.ToString
                            休息时间 = GetRandom()
                            Add访问("随机休息" & 休息时间 & "......")
                            Threading.Thread.Sleep(休息时间)
                            If CSDNLOGIN.打招呼(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 空间, 打招呼返回内容) _
                                AndAlso 打招呼返回内容.Contains("已经") _
                                AndAlso 打招呼返回内容.Trim.Length > 0 Then
                                CSDNBL.UpdateLASTSayHello(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.打招呼的个数)
                            End If
                            Add访问(打招呼返回内容)
                            gv访问.Rows(idex).Cells("访问打招呼列").Value = "成功"
                            _打招呼个数 += 1
                        Else
                            gv访问.Rows(idex).Cells("访问打招呼列").Value = "已经打过"
                        End If
                        是否打过招呼 = True
                        Continue For '这条记录不添加留言了
                    Else
                        If chk打招呼.Checked = True AndAlso _留言个数 Mod 4 = 0 AndAlso _留言个数 > 0 Then
                            是否打过招呼 = False
                        End If
                    End If

                    '每五次留言加一个朋友
                    If chk添加好友.Checked AndAlso _留言个数 Mod 5 = 0 AndAlso _留言个数 > 0 AndAlso 是否加个朋友 = False Then
                        '添加好友
                        CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.加为朋友的个数, count)
                        If count > CInt(_myConfig.每天加为朋友的个数) Then
                            chk添加好友.Enabled = False
                            chk添加好友.Checked = False
                            Add访问("今天已经到了访问限制次数了")
                        Else
                            打招呼返回内容 = ""
                            空间 = gv访问.Rows(idex).Cells("访问空间列").Value.ToString
                            休息时间 = GetRandom()
                            Add访问("随机休息" & 休息时间 & "......")
                            Threading.Thread.Sleep(休息时间)
                            If CSDNLOGIN.添加好友(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, 空间, 打招呼返回内容) _
                                AndAlso 打招呼返回内容.Contains("好友请求已经发送，请等待对方验证中") _
                                AndAlso 打招呼返回内容.Trim.Length > 0 Then
                                CSDNBL.UpdateLASTFriend(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.加为朋友的个数)
                                gv访问.Rows(idex).Cells("访问好友列").Value = "成功"
                                _添加朋友个数 += 1
                            ElseIf 打招呼返回内容.Contains("您当前的好友数目达到系统限制，请先删除部分好友") Then
                                Me.chk添加好友.Checked = False
                                Me.chk添加好友.Enabled = False
                                _myConfig.最大朋友 = "1"
                            Else
                                gv访问.Rows(idex).Cells("访问好友列").Value = "失败"
                            End If
                            Add访问(打招呼返回内容)
                            Continue For
                        End If
                    Else
                        If chk添加好友.Checked = True AndAlso _留言个数 Mod 5 = 0 AndAlso _留言个数 > 0 Then
                            是否加个朋友 = False
                        End If
                    End If

                    '估计二十分钟左右
                    If _留言个数 Mod 8 = 0 AndAlso _留言个数 > 0 Then
                        Dim lst请求好友 As New List(Of 请求好友)
                        If CSDNLOGIN.分析请求好友(lst请求好友) = True Then
                            '一次只加2个
                            Dim tempCount As Integer = 5
                            If tempCount > lst请求好友.Count Then
                                tempCount = lst请求好友.Count
                            End If
                            If lst请求好友.Count = 0 Then
                                tempCount = -1
                            End If
                            For jdex As Integer = 0 To tempCount - 1
                                CSDNLOGIN.自动添加请求的好友(lst请求好友(jdex)._uid, lst请求好友(jdex)._id, 打招呼返回内容)
                                If 打招呼返回内容.Contains("成为好友了") Then
                                    Add访问(打招呼返回内容)
                                    休息时间 = GetRandom()
                                    Add访问("随机休息" & 休息时间 & "......")
                                    Threading.Thread.Sleep(休息时间)
                                End If
                            Next
                        End If
                    End If

                    CSDNBL.GetCSDBCount(_key, CSDNBL.CSDBCount.留言个数, count)
                    If count > CInt(_myConfig.每天的留言个数) Then
                        Me.chk留言.Enabled = False
                        chk留言.Checked = False
                        Add访问("今天已经到了访问限制次数了")
                    Else
                        If chk访问空间.Checked Then
                            '休息的时间超过 70左右
                            If count_休息 > 1000 * 62 Then
                                LastCommentTime = CStr(gv访问.Rows(idex).Cells("LastComment").Value)
                                If LastCommentTime = 0 OrElse UTIL.Get两个时间间隔(现在时间, LastCommentTime) > _留言间隔时间 Then
                                    If CSDNLOGIN.留言(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, _
                                                    gv访问.Rows(idex).Cells("访问空间列").Value.ToString, 打招呼返回内容) _
                                        AndAlso 打招呼返回内容.Contains("进行的操作完成了") _
                                        AndAlso 打招呼返回内容.Trim.Length > 0 Then
                                        CSDNBL.UpdateLASTLastComment(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                        gv访问.Rows(idex).Cells("访问留言列").Value = "成功"
                                        CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.留言个数)
                                        _留言个数 += 1
                                    Else
                                        CSDNBL.UpdateLASTLastComment1(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                        gv访问.Rows(idex).Cells("访问留言列").Value = "失败"
                                    End If
                                    Add访问(打招呼返回内容)
                                Else
                                    gv访问.Rows(idex).Cells("访问留言列").Value = "已经留言过"
                                End If
                            End If
                        Else
                            LastCommentTime = CStr(gv访问.Rows(idex).Cells("LastComment").Value)
                            If LastCommentTime = 0 OrElse UTIL.Get两个时间间隔(现在时间, LastCommentTime) > _留言间隔时间 Then
                                If CSDNLOGIN.留言(gv访问.Rows(idex).Cells("访问编号列").Value.ToString, _
                                                gv访问.Rows(idex).Cells("访问空间列").Value.ToString, 打招呼返回内容) _
                                    AndAlso 打招呼返回内容.Contains("进行的操作完成了") _
                                    AndAlso 打招呼返回内容.Trim.Length > 0 Then
                                    gv访问.Rows(idex).Cells("访问留言列").Value = "成功"
                                    CSDNBL.UpdateCSDBCount(_key, CSDNBL.CSDBCount.留言个数)
                                Else
                                    CSDNBL.UpdateLASTLastComment1(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                                    gv访问.Rows(idex).Cells("访问留言列").Value = "失败"
                                End If
                                Add访问(打招呼返回内容)
                                休息时间 = GetRandom留言()
                                Add访问("随机休息" & 休息时间 & "......")
                                Threading.Thread.Sleep(休息时间)
                            Else
                                gv访问.Rows(idex).Cells("访问留言列").Value = "已经留言过"
                            End If
                            CSDNBL.UpdateLASTLastComment(gv访问.Rows(idex).Cells("访问编号列").Value.ToString)
                            _留言个数 += 1
                        End If

                    End If
                End If

                    If count_访问空间 = 40 Then
                        count_访问空间 = 0
                    End If

                    If count_休息 > 1000 * 62 Then
                        count_休息 = 0
                    End If

                    If _访问中 Then
                        BW访问.ReportProgress(idex)
                    Else
                        Exit Sub
                    End If
            Catch ex As Exception

            End Try
        Next

    End Sub

    Private Sub BW采集_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BW访问.ProgressChanged
        PB访问.Value = e.ProgressPercentage
    End Sub

    Private Sub BW采集_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BW访问.RunWorkerCompleted
        _访问中 = False
        btn开始访问.Enabled = True
        btn停止访问.Enabled = False
        PB访问.Value = 0
        Me.LBL访问.Text = ""
    End Sub

    Private Sub 打开空间ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开空间ToolStripMenuItem.Click
        If gv访问.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Process.Start(gv访问.SelectedRows(0).Cells("访问空间列").Value.ToString)
    End Sub

    Private _采集 As Boolean = False
    Private Sub BW采集_DoWork_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BW采集.DoWork

        '采集的时候不用登陆
        '登陆
        'If CSDNLOGIN.是否登录 = False Then
        '    Dim bLogin As Boolean = CSDNLOGIN.Login(_myConfig.用户名, _myConfig.密码)
        '    If Not bLogin Then
        '        Add采集("登陆失败")
        '    End If
        'End If
        'Add采集("登陆成功")
        Try
            Dim lstFriend As New List(Of MyFriend)
            Dim lstComment As New List(Of MyFriend)
            Dim dgr As DataGridViewRow
            Dim 休息时间 As Integer = 0
            Dim bCount As Integer = 0
            Dim 编号 As String = ""
            For idex As Integer = 0 To gv采集.Rows.Count - 1
                编号 = gv采集.Rows(idex).Cells("采集编号列").Value.ToString()
                dgr = gv采集.Rows(idex)
                lst采集.Items.Insert(0, dgr.Cells("SPACEADDRESS").Value.ToString())

                dgr.Selected = True
                gv采集.FirstDisplayedScrollingRowIndex = idex

                休息时间 = GetRandom()
                Add采集("随机休息" & 休息时间 & "......")
                Threading.Thread.Sleep(休息时间)

                '连续访问50个休息10秒
                If idex Mod 30 = 0 AndAlso idex > 0 Then
                    Add采集("连续访问30个 休息 10 秒 防止拒绝访问")
                    System.Threading.Thread.Sleep(10000)
                End If

                If CSDNLOGIN.登陆空间(dgr.Cells("SPACEADDRESS").Value.ToString(), lstFriend, lstComment) Then
                    If lstFriend.Count > 0 Then
                        _ds.CSDN1000.Rows.Clear()
                        For Each myFrend As MyFriend In lstFriend
                            Dim dr As CSDN1000DataSet.CSDN1000Row = Me._ds.CSDN1000.NewCSDN1000Row
                            dr.ID = myFrend.id
                            dr.NAME = myFrend.name
                            dr.username = myFrend.username
                            dr.BLOGADDRESS = ""
                            dr.SPACEADDRESS = myFrend.Url
                            dr.IsFriend = False
                            dr.AddTime = Now.ToString("yyyyMMddHHmmss")
                            dr.UpdateTime = 0
                            dr.LastAccessTime = 0
                            dr.LastComment = 0
                            dr.LASTSayHello = 0
                            dr.LastFriend = 0
                            dr.LastSetFocus = 0

                            Me._ds.CSDN1000.AddCSDN1000Row(dr)
                        Next
                    End If
                    CSDNBL.UpdateData(Me._ds, bCount)

                End If
                _采集个数 += bCount
                LBL采集个数.Text = "采集了" & _采集个数 & "个朋友"
                Add采集("采集了" & bCount & "个朋友")
                CSDNBL.UpdateUserName(编号)

                If _采集 Then
                    BW采集.ReportProgress(idex)
                Else
                    Exit Sub
                End If
            Next
        Catch ex As Exception

        End Try
     
    End Sub

    Private Sub BW采集_ProgressChanged_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BW采集.ProgressChanged
        PB采集.Value = e.ProgressPercentage
    End Sub

    Private Sub BW采集_RunWorkerCompleted_1(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BW采集.RunWorkerCompleted
        _采集 = False
        btn采集.Enabled = True
        btn结束采集.Enabled = False
        PB采集.Value = 0
        LBL采集个数.Text = ""
    End Sub

    Private Sub 计时器ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 计时器ToolStripMenuItem.Click
        Using frm As New CSDNCount
            frm.ShowDialog()
        End Using
    End Sub

    Private Sub 留言ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 留言ToolStripMenuItem.Click
        Using frm As New CSDNComment
            frm.ShowDialog()
        End Using
    End Sub

    Private Sub 显示ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 显示ToolStripMenuItem.Click
        Me.Visible = Not Me.Visible

    End Sub

    Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click
        If MessageBox.Show("是否退出程序？", "CSDN小工具", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Return
        End If
        _bClose = True
        Me.Close()
    End Sub

    Private _bClose As Boolean = False
    Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If _bClose = False AndAlso e.CloseReason <> CloseReason.WindowsShutDown Then
            e.Cancel = True
            Me.Hide()
            Exit Sub
        End If

        NotifyIcon1.Visible = False
        NotifyIcon1.Dispose()
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Visible = Not Me.Visible
    End Sub

    Private Sub 留言论坛ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 留言论坛ToolStripMenuItem.Click

    End Sub

    Private Sub 退出ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem2.Click
        If MessageBox.Show("是否退出程序？", "CSDN小工具", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
            Return
        End If
        _bClose = True
        Me.Close()
    End Sub

    Private Sub MainForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        'If _myConfig.登陆后留言的动作 = "1" Then
        '    btn开始访问_Click(sender, e)
        '    Threading.Thread.Sleep(1500)
        'End If

        'If _myConfig.登陆后采集的动作 = "1" Then
        '    btn采集_Click(sender, e)
        'End If

        'If _myConfig.是否自动登陆 = True Then

        'End If

    End Sub

End Class
